<?php
    //
    //  Created by Chagan Anton on 29.07.12.
    //  Copyright Chagan Anton 2012. All rights reserved.
    //
/**
 * This is the model class for table "contacts".
 */
class Contacts extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'contacts':
	 * @var integer $c_id
	 * @var string $c_name
	 * @var string $c_first_name
	 * @var string $c_surname
	 * @var string $c_second_name
	 * @var integer $c_user_id
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return Contacts the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'contacts';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('c_name, c_user_id', 'required'),
			array('c_user_id', 'numerical', 'integerOnly'=>true),
			array('c_name, c_first_name, c_surname, c_second_name', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('c_id, c_name, c_first_name, c_surname, c_second_name, c_user_id', 'safe', 'on'=>'search'),
		);
	}
	
	public function beforeValidate() {
		parent::beforeValidate();
		$this->c_user_id = Yii::app()->user->id;
		return true;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cUser' => array(self::BELONGS_TO, 'Users', 'c_user_id'),
			'contactsOptions' => array(self::HAS_MANY, 'ContactsOptions', 'co_contact_id'),
			'projects' => array(self::MANY_MANY, 'Projects', 'contacts_projects(cp_contact_id, cp_project_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'c_id' => '#',
			'c_name' => 'Contact Name',
			'c_first_name' => 'First Name',
			'c_surname' => 'Surname',
			'c_second_name' => 'Second Name',
			'c_user_id' => 'User Id',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('c_id',$this->c_id);

		$criteria->compare('c_name',$this->c_name,true);

		$criteria->compare('c_first_name',$this->c_first_name,true);

		$criteria->compare('c_surname',$this->c_surname,true);

		$criteria->compare('c_second_name',$this->c_second_name,true);

		$criteria->compare('c_user_id',$this->c_user_id);

		$criteria->condition = 'c_user_id=:user_id';
		$criteria->params = array(':user_id'=>Yii::app()->user->id);
		
		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}